let mapName = 'tianxin' //地图名称   //修改地图数据记得修改地图名称
const dbInit = () => {
  return new Promise((resolve, reject) => {
    const connection = window.indexedDB.open('sceneStorage', 2)
    connection.onsuccess = function (event) {
      resolve(event.target.result)
    }
    connection.onupgradeneeded = function (event) {
      const db = event.target.result
      if (!db.objectStoreNames.contains('person')) {
        db.createObjectStore('person', { autoIncrement: true })
      }
    }
    connection.onerror = function (err) {
      reject(err)
    }
  })
}
const dbOperation = async (type, data) => {
  let typeList = ['add', 'delete', 'clear', 'put', 'get', 'getAll']
  if (!typeList.includes(type)) {
    throw new Error(`操作类型错误, 仅支持: ${typeList.toString()} 方法`)
  }
  const readType =
    type === 'add' || type === 'clear' || type === 'delete'
      ? 'readwrite'
      : 'readonly'
  const res = await dbInit()
  const objectStore = res.transaction('person', readType).objectStore('person')
  const response = new Promise((resolve, reject) => {
    const request = objectStore[type](data)
    request.onsuccess = (res) => {
      resolve(res.target.result)
    }
    request.onerror = (err) => {
      reject(err)
    }
  })
  return response
}

export { dbOperation, mapName }
